-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add targets option to @babel/plugin-transform-runtime #11572
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit d71fef1:
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/23389/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
One additional useful thing would be the ability to prevent the inclusion of a given polyfill (like JSON.stringify
/json/stringify
).
...orm-runtime/test/fixtures/runtime-corejs3/built-in-static-methods-esmodules-target/output.js
Show resolved
Hide resolved
I think that's a good idea. We could mirror |
Co-authored-by: Huáng Jùnliàng <jlhwung@gmail.com>
@JLHwung is there any interest in this PR anymore? If not, then I can close it |
@TomerAberbach As a side effect of #12845, now this plugin supports const input = String.raw`
Array.from(3);
foo.flatMap(fn);
`;
const out = babel.transform(input, {
configFile: false,
sourceType: "module",
targets: "chrome 60",
plugins: [[transformRuntime, { corejs: 3 }]],
});
console.log(out.code); logs import _flatMapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat-map";
Array.from(3);
_flatMapInstanceProperty(foo).call(foo, fn); while const input = String.raw`
Array.from(3);
foo.flatMap(fn);
`;
const out = babel.transform(input, {
configFile: false,
sourceType: "module",
targets: "chrome 40",
plugins: [[transformRuntime, { corejs: 3 }]],
});
console.log(out.code); logs import _Array$from from "@babel/runtime-corejs3/core-js-stable/array/from";
import _flatMapInstanceProperty from "@babel/runtime-corejs3/core-js-stable/instance/flat-map";
_Array$from(3);
_flatMapInstanceProperty(foo).call(foo, fn); Thanks for the PR anyway! |
@babel/plugin-transform-runtime
is great because it can be used to polyfill features without polluting the global namespace (i.e. ponyfills). This is the ideal choice for libraries because they shouldn't pollute the global namespace. However,@babel/plugin-transform-runtime
does not support only polyfilling the features required by the target browser/node versions like@babel/preset-env
does. This results in unnecessarily large bundles containing unused polyfills.This pull request adds a
targets
option to@babel/plugin-transform-runtime
that behaves similarly to@babel/preset-env
'stargets
field. It also addsconfigPath
andignoreBrowserslist
options like@babel/preset-env
has.With regards to dependencies,
@babel/compat-data
,@babel/helper-compilation-targets
, andcore-js-compat
dependencies were added to@babel/plugin-transform-runtime
.Lastly, I'm aware that Babel is rethinking its polyfilling story so this change may become obsolete in the future. However, I think people have been waiting a long time for this feature and it could be a while before Babel's new polyfilling approach is ready; so I think this feature is useful as a short-term solution.
Looking forward to your feedback!